<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$logtype = intval($_GPC['logtype']);
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

if ($operation == 'display') {
    if ($setting['auth_mode'] == 1 || empty($setting)) {
        $method = 'commission_form'; //method
        $host = $this->getOAuthHost();
        $authurl = $host . 'app/' . $this->createMobileUrl($method, array(), true) . '&authkey=1';
        $url = $host . 'app/' . $this->createMobileUrl($method, array(), true);
        if (isset($_COOKIE[$this->_auth2_openid])) {
            $from_user = $_COOKIE[$this->_auth2_openid];
            $nickname = $_COOKIE[$this->_auth2_nickname];
            $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
        } else {
            if (isset($_GPC['code'])) {
                $userinfo = $this->oauth2($authurl);
                if (!empty($userinfo)) {
                    $from_user = $userinfo["openid"];
                    $nickname = $userinfo["nickname"];
                    $headimgurl = $userinfo["headimgurl"];
                } else {
                    message('授权失败!');
                }
            } else {
                if (!empty($this->_appsecret)) {
                    $this->getCode($url);
                }
            }
        }
    } else {
        load()->model('mc');
        if (empty($_W['fans']['nickname'])) {
            mc_oauth_userinfo();
        }
        $from_user = $_W['fans']['openid'];
        $nickname = $_W['fans']['nickname'];
        $headimgurl = $_W['fans']['tag']['avatar'];
    }


    if (empty($from_user)) {
        message('会话已过期，请重新发送关键字!');
    }

    $user = pdo_fetch("SELECT * FROM " . tablename($this->table_fans) . " WHERE weid = :weid  AND from_user=:from_user ORDER BY `id` DESC limit 1", array(':weid' => $weid, ':from_user' => $from_user));

    $commission_price = floatval($user['commission_price']);
    $commission_price = sprintf('%.2f', $commission_price);

    $delivery_price = floatval($user['delivery_price']);
    $delivery_price = sprintf('%.2f', $delivery_price);
    $delivery_rate = floatval($setting['delivery_rate']);

    //市场专员
    $marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
    if (empty($marketing)) {
        message('您不是市场专员，无权访问！');
    }

    $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid=:weid AND
marketingid=:marketingid;", array(':weid' => $weid, 'marketingid' => $marketing['id']));
    $storeids_arr = array();
    foreach ($list as $key => $value) {
        $storeids_arr[] = $value['id'];
    }
    $storeids = implode(",", $storeids_arr);

    //所有佣金
    $all_marketing_price = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE
weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids})", array(':weid' => $weid));
    $all_marketing_price = sprintf('%.2f', $all_marketing_price);

    $check_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=0 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
    $check_marketing_price = sprintf('%.2f', $check_marketing_price);

    $ischeck_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=1 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
    $ischeck_marketing_price = sprintf('%.2f', $ischeck_marketing_price);
    $marketing_price = $all_marketing_price - $check_marketing_price - $ischeck_marketing_price;
    $marketing_price = sprintf('%.2f', $marketing_price);


    include $this->template($this->cur_tpl . '/marketing_form');
} else if ($operation == 'post') {
    if (empty($from_user)) {
        $this->showTip('请重新发送关键字进入系统!');
    }

    //市场专员
    $marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
    if (empty($marketing)) {
        $this->showTip('您不是市场专员，无权访问！');
    }

    $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid=:weid AND
marketingid=:marketingid;", array(':weid' => $weid, 'marketingid' => $marketing['id']));
    $storeids_arr = array();
    foreach ($list as $key => $value) {
        $storeids_arr[] = $value['id'];
    }
    $storeids = implode(",", $storeids_arr);

    //所有佣金
//    $all_marketing_price = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE
//weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids})", array(':weid' => $weid));
//    $all_marketing_price = sprintf('%.2f', $all_marketing_price);
//
//    $check_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
//weid=:weid AND status=0 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
//    $check_marketing_price = sprintf('%.2f', $check_marketing_price);
//
//    $ischeck_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
//weid=:weid AND status=1 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
//    $ischeck_marketing_price = sprintf('%.2f', $ischeck_marketing_price);
//    $marketing_price = $all_marketing_price - $check_marketing_price - $ischeck_marketing_price;
//    $marketing_price = sprintf('%.2f', $marketing_price);

    //所有佣金
    $all_marketing_price = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE
weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids})", array(':weid' => $weid));
    $all_marketing_price = sprintf('%.2f', $all_marketing_price);

    $check_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=0 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
    $check_marketing_price = sprintf('%.2f', $check_marketing_price);

    $ischeck_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=1 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
    $ischeck_marketing_price = sprintf('%.2f', $ischeck_marketing_price);
    $marketing_price = $all_marketing_price - $check_marketing_price - $ischeck_marketing_price;
    $marketing_price = sprintf('%.2f', $marketing_price);


    $totalprice = floatval($_GPC['totalprice']);

    if ($marketing_price <= 0) {
        $this->showTip('你当前没有佣金!' . $marketing_price);
    }

    if ($totalprice > $marketing_price) {
        $this->showTip('提现的金额不能大于您的佣金！');
    }

    $data = array(
        'weid' => $weid,
        'storeid' => $storeid,
        'marketingid' => $marketing['id'],
        'price' => $totalprice,
        'status' => 0,
        'handletime' => 0,
        'dateline' => TIMESTAMP
    );

    pdo_insert('weisrc_dish_marketinglog', $data);

    $this->showTip('已申请提现请等待管理员审核！', 1);
}

